//
//  CollectionViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/21.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "CollectionViewController.h"
#import "BaseDataRequest.h"
#import "ArticleDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "CLUIBaseTableViewController+Empty.h"

@interface CollectionViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@property (assign, nonatomic) NSInteger page;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收藏列表";
    [self showNavgationWithLeftItemWithTitle:@"我的"];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf getArticleList];
    }];
    [SVProgressHUD showWithStatus:@"获取收藏列表.." maskType:SVProgressHUDMaskTypeClear];
    self.page = 1;
    [self getArticleList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"UNDATETABLEVIEW" object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)getArticleList{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (![self logInspection]) {
        [dict setObject:@"0" forKey:@"user_id"];
    }else{
        [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    }
    [dict setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
    WS(weakSelf);
    [[BaseDataRequest alloc] articleCollectList:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        [self.tableView.footer endRefreshing];
        if ([status isEqualToString:@"0"]) {
            NSMutableArray *arrResult = [NSMutableArray arrayWithArray:[responseObject objectForKey:@"result"]];
            [self.arrDataSource addObjectsFromArray:arrResult];
            if (arrResult.count == 0 || arrResult.count < 10) {
                if(self.arrDataSource.count == 0){
                    [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"这里还没有内容" withImage:[UIImage imageNamed:@"icon_ blank"]];
                    self.tableView.footer.hidden = YES;
                }else{
                    [self.tableView.footer noticeNoMoreData];
                }
            }
            [self.tableView reloadData];
        }else{
            if (self.tableView.footer.isRefreshing) {
                if (self.page>0) {
                    self.page--;
                }
            }
            if(self.arrDataSource.count == 0){
                [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"出错了，稍后再试吧" withImage:[UIImage imageNamed:@"icon_wifi"]];
                self.tableView.footer.hidden = YES;
            }else{
                if (error != nil) {
                    TIPSFORVIEW(@"请检测网络设置");
                }else{
                    TIPSFORVIEW([responseObject objectForKey:@"message"]);
                }
            }  
        }
    }];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"CollectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    UIImageView *imageV = [cell viewWithTag:1];
    UILabel *labName = [cell viewWithTag:2];
    UILabel *labFrom = [cell viewWithTag:3];
    UILabel *labTime = [cell viewWithTag:4];
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    [imageV sd_setImageWithURL:[NSURL URLWithString:[dicr objectForKey:@"thumbimage"]] placeholderImage:[UIImage imageNamed:@"iocn_default"]];
    labFrom.text= [NSString stringWithFormat:@"文章来源：%@",[dicr objectForKey:@"sourcename"]];
    labName.text = [dicr objectForKey:@"title"];
    labTime.text = [dicr objectForKey:@"createtime"];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    ArticleDetailViewController *articleDetail = [[UIStoryboard storyboardWithName:@"ArticleViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ArticleDetailViewController"];
    articleDetail.strArticleURL = [dicr objectForKey:@"href"];
    articleDetail.strArticleID = [dicr objectForKey:@"article_id"];
    [self.navigationController pushViewController:articleDetail animated:YES];
}

#pragma mark - set/get
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

#pragma mark - 私有方法
- (BOOL)logInspection{
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
    if ([str isEqualToString:@""] || str == nil || str == NULL) {
        return NO;
    }else{
        return YES;
    }
}

- (void)updateTable{
    self.page = 1;
    [self.arrDataSource removeAllObjects];
    [self getArticleList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
